﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Manager_ListAssess : System.Web.UI.Page
{
    CommonClass ccobj = new CommonClass();
    DBClass dbobj = new DBClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            bindgrid();
    }
    protected void bindgrid()
    {
        string strsql = "select * from Assess";
        DataTable dstable = dbobj.GetDataSet(strsql, "tabassess");
        GridView1.DataSource = dstable.DefaultView;
        GridView1.DataKeyNames = new string[] { "AssessID" };
        GridView1.DataBind();
        DropDownList ddl;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            DataRowView drv = dstable.DefaultView[i];
            ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList2");
            ddl.SelectedValue = drv["DepartmentID"].ToString();
        }
        CheckBox chk;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        { 
            DataRowView drv = dstable.DefaultView[i];
            chk = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (Convert.ToInt32(drv["IsManager"]) == 1)
                chk.Checked = true;
            else
                chk.Checked = false;
        }
    }
    public DataTable ddlbind()
    {
        string strdepart = "select * from Department ";
        DataTable db = dbobj.GetDataSet(strdepart, "tabdepart");
        return db;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "")
            Response.Write(ccobj.MessageBox("请输入关键字!"));
        else
        {
            string strsql = "select * from Teacher where ";
            switch (DropDownList1.SelectedValue.ToString().Trim())
            {
                case "工号":
                    strsql += "AssessID like '%" + TextBox1.Text.Trim() + "%'";
                    break;
                case "姓名":
                    strsql += "AssessName like '%" + TextBox1.Text.Trim() + "%'";
                    break;
                case "部门":
                    string strid = dbobj.ExecScalar("select DepartmentID from Department where DepartmentName like '%" + TextBox1.Text.Trim() + "%'");
                    strsql += "DepartmentID='" + strid + "'";
                    break;
            }
            DataTable dstable = dbobj.GetDataSet(strsql, "tabassess");
            GridView1.DataSource = dstable.DefaultView;
            GridView1.DataKeyNames = new string[] { "AssessID" };
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "update")
        {
            DropDownList ddl = (DropDownList)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("DropDownList2");
            CheckBox chb = (CheckBox)GridView1.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("CheckBox1");
            string strsql = "update Assess set DepartmentID='" + ddl.SelectedValue.ToString() + "',IsManager=" + (chb.Checked ? 1 : 0) + " where AssessID='" + GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString() + "'";
            dbobj.ExecNonQuery(strsql);
            Response.Write(ccobj.MessageBox("更新成功！"));
            if (chb.Checked)
            {
                string strsql1 = "select * from Admin where AdminID='" + GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString() + "'";
                DataTable dstable = dbobj.GetDataSet(strsql1, "tabadmin");
                if (dstable.Rows.Count > 0)
                {
                    string strsql2 = "update Admin set DepartmentID='" + ddl.SelectedValue.ToString() + "' where AdminID='" + GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString() + "'";
                    dbobj.ExecNonQuery(strsql2);
                    Response.Write(ccobj.MessageBox("管理员表更新成功"));
                }
                else
                {
                    string strname = dbobj.ExecScalar("select AssessName from Assess where AssessID='" + GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString() + "'");
                    string strsql3 = "insert into Admin (AdminID,AdminName,PassWord,DepartmentID,IsAssessMan) values('" + GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString() + "','" + strname + "','" + GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString() + "','" + ddl.SelectedValue.ToString() + "'," + 0 + ")";
                    dbobj.ExecNonQuery(strsql3);
                    Response.Write(ccobj.MessageBox("已将管理员添加到管理员表！"));
                }
            }
            else
            {
                string strsql4 = "delete from Admin where AdminID='" + GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString() + "'";
                dbobj.ExecNonQuery(strsql4);
                Response.Write(ccobj.MessageBox("已将该用户从管理员表中删除！"));
            }
        }
    }
}